/*
 *
 * author : Mishal Parmar
 * Date: 20 Oct 2012
 */
package controllers;

import dbhandler.EventManage;
import entityclasses.event;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 *
 * @author admin
 */
@WebServlet(name = "createevent", urlPatterns = {"/createevent"})
public class createevent extends HttpServlet {

    /**
     * Processes requests for both HTTP
     * <code>GET</code> and
     * <code>POST</code> methods.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        java.util.Date dt = new java.util.Date();
        java.text.SimpleDateFormat sdf = new java.text.SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String currentTime = sdf.format(dt);
        try {
            /* TODO output your page here. You may use following sample code. */
            
            
            out.println("<html>");
            out.println("<head>");
            out.println("<title>Servlet createevent</title>");            
            out.println("</head>");
            out.println("<body>");
            event newevent=new event();
        
        newevent.setAccessId(Integer.parseInt(request.getParameter("accesstype")));
        newevent.setTypeId(Integer.parseInt(request.getParameter("eventtype")));
        newevent.setPosteddt(currentTime);
        
        
                      	        
        
        EventManage add= new EventManage();
        
         if(add.addeventquery(newevent))
               out.print("Done");
               
           String eventselect="";
           RequestDispatcher rd;
           if(newevent.getTypeId()==1)
           {
               eventselect="http://localhost:8084/evtsur/eventpage/createelection.jsp";
                      //request.setAttribute("electionid", rd);
                     // rd=request.getRequestDispatcher(/eventpage/createelection.jsp);
                    //  rd.forward(request, response);
           }
         
   
           else if(newevent.getTypeId()==2)
           {
            eventselect="http://localhost:8084/evtsur/Survey_managment/Survey_forms/Createsurvey.jsp";
            
           }
           
           
            String site = new String("http://localhost:8084/evtsur/Survey_managment/Survey_forms/Createsurvey.jsp");
            response.setStatus(response.SC_MOVED_TEMPORARILY);
            response.setHeader("Location", site); 
           //eventselect="/Survey_managment/Survey_forms/Createsurvey.jsp";
             //         RequestDispatcher rd=request.getRequestDispatcher(eventselect);
           //rd.forward(request, response);
           // out.println("<h1>Servlet createevent at " + request.getContextPath() + "</h1>");
            out.println("</body>");
            out.println("</html>");
        } finally {            
            out.close();
        }
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /**
     * Handles the HTTP
     * <code>GET</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Handles the HTTP
     * <code>POST</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
        /*
        event newevent=new event();
        
        newevent.setAccessId(Integer.parseInt(request.getParameter("useraccess")));
        newevent.setTypeId(Integer.parseInt(request.getParameter("accesstype")));
        
                      	        
        PrintWriter out = response.getWriter(); 
        
        out.println(newevent.getAccessId());
        out.println(newevent.getTypeId());
        
       // AddEvent add= new AddEvent();
        
      //   add.addeventquery(newevent);
       //        out.print("Done");
        */
    }

    /**
     * Returns a short description of the servlet.
     *
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}
